import React, { useState, useReducer } from "react";

const initState = {
  count: 3,
};

const reducer = (state, action) => {
  switch (action.type) {
    case "add":
      return {
        count: state.count + action.payload,
      };
    default:
  }
};

export default function Index() {
  const [state, dispatch] = useReducer(reducer, initState);

  const chagneCount = () => {
    dispatch({
      type: "add",
      payload: 2,
    });
  };

  return (
    <div>
      <h1>useReducer</h1>
      <p>count:{state.count}</p>
      <input type="button" value="修改count" onClick={chagneCount} />
    </div>
  );
}
